package com.derbysoft.lowcostadapter.dswitch3.translate.impl;

import com.derbysoft.dswitch.dto.hotel.common.TotalDTO;
import com.derbysoft.dswitch.dto.hotel.reservation.HotelReservationDTO;
import com.derbysoft.dswitch.dto.hotel.reservation.HotelReservationRS;
import com.derbysoft.dswitch.remote.hotel.dto.ErrorDTO;
import com.derbysoft.dswitch.remote.hotel.dto.HotelReservationRequest;
import com.derbysoft.dswitch.remote.hotel.dto.HotelReservationResponse;
import com.derbysoft.lowcostadapter.commons.dto.BookResponse;
import com.derbysoft.lowcostadapter.commons.dto.ReturnStatus;
import com.derbysoft.lowcostadapter.dswitch2.service.helper.Constant;
import com.derbysoft.lowcostadapter.dswitch3.translate.Translator;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

/**
 * Created by: jason
 * Date: 2012-06-26
 */
@Component("ds3ReservationRSTranslator")
public class DSwitch3ReservationRSTranslator extends AbstractDSwitch3Translator
        implements Translator<BookResponse, HotelReservationResponse, HotelReservationRequest> {
    @Override
    public HotelReservationResponse translate(BookResponse bookResponse, HotelReservationRequest reservationRequest) {
        HotelReservationResponse hotelReservationResponse = new HotelReservationResponse();
        hotelReservationResponse.setTaskId(reservationRequest.getHeader().getTaskId());
        ReturnStatus returnStatus = bookResponse.getBookResult().getReturnStatus();
        if (!returnStatus.isSuccess() || StringUtils.isNotBlank(returnStatus.getException())) {
            hotelReservationResponse.setError(createErrorDTO(bookResponse));
            return hotelReservationResponse;
        }
        hotelReservationResponse.setHotelReservationRS(createReservationRS(bookResponse, reservationRequest));
        return hotelReservationResponse;
    }

    private ErrorDTO createErrorDTO(BookResponse bookResponse) {
        ErrorDTO errorDTO = new ErrorDTO();
        errorDTO.setCode(Constant.SYSTEM_ERROR);
        errorDTO.setMessage(bookResponse.getBookResult().getReturnStatus().getException());
        errorDTO.setSource(provider);
        return errorDTO;
    }

    private HotelReservationRS createReservationRS(BookResponse bookResponse, HotelReservationRequest reservationRequest) {
        HotelReservationRS hotelReservationRS = new HotelReservationRS();
        hotelReservationRS.setErsp(reservationRequest.getHotelReservationRQ().getErsp());
        HotelReservationDTO hotelReservation = reservationRequest.getHotelReservationRQ().getHotelReservation();
        hotelReservation.getRoomStaysList().get(0).setHotelReservationNo(bookResponse.getBookResult().getBookingReference());
        hotelReservationRS.setHotelReservation(hotelReservation);
        hotelReservationRS.getHotelReservation().getRoomStaysList().get(0).setTotal(createTotal(bookResponse));
        return hotelReservationRS;
    }

    private TotalDTO createTotal(BookResponse bookResponse) {
        TotalDTO totalDTO = new TotalDTO();
        totalDTO.setCurrency(DEFAULT_CURRENCY);
        totalDTO.setAmountBeforeTax(bookResponse.getBookResult().getTotalPrice().doubleValue());
        return totalDTO;
    }
}
